# Category=Other

#@ Interface for NET_snmp toolkit.
# Author: oil oil@pp.inet.fi

#
# Version history:
# version	date	author	explanation
# v.0.0.50	090207	oil	First test version
# v.0.0.51	090315	oil	First test version
#
#
# Todo list:
# version	date	author	explanation
#
#

&Reload_pre_add_hook(\&net_snmp_stop) if $Reload;

sub net_snmp_stop {
	stop  $p_net_snmp;
        file_write("$net_snmp.txt", 'Stoped by reload clear');
}

$p_net_snmp = new Process_Item;
# $p_net_snmp -> restore_data('pid');
# $Save{'p_net_snmp_pid'} = pid $p_net_snmp;

my $net_snmp   = "$config_parms{data_dir}/NET_snmp";
my $snmpdelta  = "$Pgm_Root/../code/Net_SNMP/bin/snmpdelta"; # More than one dir in $config_parms{code_dir}! Do someting!
my @ifInOctets;
my @ifOutOctets;
my @ifInBps;
my @ifOutBps;
my $file_max_size = 10000;

$s_inbps	= new Generic_Item;
$s_outbps	= new Generic_Item;
&tk_entry('ADSL in speed',  \$ifInBps[4]);
&tk_entry('ADSL out speed', \$ifOutBps[4]);
&tk_entry('Eth. in speed',  \$ifInBps[2]);
&tk_entry('Eth. out speed', \$ifOutBps[2]);

if ($Reload) {
#	print "snmp: PID = " . $Save{'p_net_snmp_pid'} . "*******************************************\n";
}

if (not defined pid $p_net_snmp) {
        file_write("$net_snmp.txt", 'Start file clear');
	$p_net_snmp ->set_output("$net_snmp.txt");
	set   $p_net_snmp "$snmpdelta.exe -c public -v 2c -CT $config_parms{net_snmp_ip_address} ifInOctets.4 ifOutOctets.4 ifInOctets.2 ifOutOctets.2";
#	set_timeout $p_net_snmp 10;
	start $p_net_snmp;
#	$Save{'p_net_snmp_pid'} = pid $p_net_snmp;
	print "Starting net_snmp $snmpdelta (pid=" . pid $p_net_snmp . ")\n";
	print_log "Starting net_snmp $snmpdelta (pid=" . pid $p_net_snmp . ")\n";
}


if (new_second 1) {
	split("\t", file_tail("$config_parms{data_dir}/net_snmp.txt", 1));
	$ifInOctets[4]  = $_[1];
	$ifOutOctets[4] = $_[2];
	$ifInOctets[2]  = $_[3];
	$ifOutOctets[2] = $_[4];
	$ifInBps[4]  = sprintf '%4.3f', $ifInOctets[4]  * 8 / 1000; #48 * 8 / 53000; 376 / 100000
	$ifOutBps[4] = sprintf '%4.3f', $ifOutOctets[4] * 8 / 1000; #48 * 8 / 53000; 376 / 100000
	$ifInBps[2]  = sprintf '%.3f', $ifInOctets[2]  * 8 / 1000;
	$ifOutBps[2] = sprintf '%.3f', $ifOutOctets[2] * 8 / 1000;

	if (file_size("$net_snmp.txt") > $file_max_size ) {
		print "Net_snmp.txt file size is " . file_size("$net_snmp.txt") . ". Stopping net_snmp $snmpdelta (pid=" . pid $p_net_snmp . ")\n";
		stop  $p_net_snmp;
	        file_write("$net_snmp.txt", 'Stopped by size limit clear');
		print_log "$net_snmp.txt size limit";
		print_log "ADSL in speed  $ifInBps[4], ADSL out speed $ifOutBps[4]";
		print_log "Eth. in speed  $ifInBps[2], Eth. out speed $ifOutBps[2]";
	}	
}

